<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\libs\Qqmta;
use think\Request;
use app\model\order\OrderItemModel;
use app\model\order\OrderModel;
use app\model\goods\GoodsModel;
use think\facade\Db;

/**
 * 统计分析
 * Class Analysis
 * @package app\controller
 */
class Analysis extends BaseEncryptController
{
    /**
     * 用户分析-实时数据
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserRealtime($start_time = '', $end_time = '')
    {
        $mta = new Qqmta();
        if (empty($start_time) || empty($end_time)) {
            $start_time = date('Y-m-d', time());
            $end_time = date('Y-m-d', time());
            $data = $mta->getHistory($start_time, $end_time);
        } else {
            $data = $mta->getRealTime();
        }
        $pv = array_sum(array_column($data['data'], 'pv'));
        $uv = array_sum(array_column($data['data'], 'uv'));
        $vv = array_sum(array_column($data['data'], 'vv'));
        $iv = array_sum(array_column($data['data'], 'iv'));
        $data = ['code' => $data['code'], 'data' => ['pv' => $pv, 'uv' => $uv, 'vv' => $vv, 'iv' => $iv]];
        return self::returnMsg($data);
    }

    /**
     * 用户分析-历史趋势
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserHistory($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getHistory($start_time, $end_time);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-新老用户
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserCompare($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getUserCompare($start_time, $end_time);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-分享分析
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserShare($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getShare($start_time, $end_time);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-地域分析
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserArea($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getArea($start_time, $end_time);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-终端分析
     * @param string $start_time
     * @param string $end_time
     * @param $type
     * @return \think\response\Json
     */
    public function getUserTerminal($start_time, $end_time, $type)
    {
        $mta = new Qqmta();
        $data = $mta->getTerminal($start_time, $end_time, $type);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-网络分析
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserNetwork($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getNetwork($start_time, $end_time);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-使用时段分析
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserPeriod($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getPeriod($start_time, $end_time);
        return self::returnMsg($data);
    }

    /**
     * 用户分析-机型分析
     * @param string $start_time
     * @param string $end_time
     * @return \think\response\Json
     */
    public function getUserMachine($start_time, $end_time)
    {
        $mta = new Qqmta();
        $data = $mta->getMachine($start_time, $end_time);
//        $data_list = $data['data'];
        return self::returnMsg($data);
    }

    # 用户分析-销售分析
    public function getUserSale($start_time, $end_time)
    {
        # 分类商品统计
        $cate = Db::name("order_item")->alias('a')
            ->whereBetweenTime('a.create_time', $start_time, $end_time)
            ->where('c.delete_time', 0)
            ->join('t_goods b', 'a.goods_id=b.id')
            ->join('t_goods_type c', 'b.goods_type_id=c.id')
            ->group('c.id')
            ->field('c.name,count(a.id) as order_num,sum(a.order_total) as order_total')
            ->select();
        # 商品统计
        $goods_list = Db::name("order_item")->alias('a')
            ->whereBetweenTime('a.create_time', $start_time, $end_time)
            ->where('a.delete_time', 0)
            ->join('t_goods b', 'a.goods_id=b.id')
            ->group('a.goods_id')
            ->field('b.goods_title_desc as name,b.goods_title as back_name,count(a.id) as goods_num,sum(a.order_total) as goods_total')
            ->order('goods_total desc,goods_num desc')
            ->limit(10)
            ->select();
        $goods = [];
        foreach ($goods_list as $key => $value) {
            if (empty($value['name'])) {
                $value['name'] = mb_substr($value['back_name'], 0, 8);
            }
            unset($value['back_name']);
            $goods[] = $value;
        }
        # 用户消费情况
        $user_count = Db::name("user")->where(['delete_time' => 0, 'status' => 1])->count();
        $cost = Db::name("order")
            ->whereBetweenTime('create_time', $start_time, $end_time)
            ->where('delete_time', 0)
            ->group('create_date_day')
            ->field('count(id) as cost,create_date_day as name')
            ->select()->toArray();
        foreach ($cost as $key => $value) {
            $cost[$key]['cost'] = round(($value['cost'] / $user_count * 100), 2);
        }
        return self::returnMsg(1, ['cate' => $cate, 'goods' => $goods, 'cost' => $cost]);
    }

    # 商品统计 2020-07-09
    public function getUserGoods($keyword = '', $timeStr = '', $page = 1, $limit = 10, $field = "order_sales_num", $order = 'desc')
    {
        $order_model = new GoodsModel();
        $data = $order_model->getGoodsData($keyword, $timeStr, $page, $limit, $field, $order);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 经营数据
     * @param string $timeStr
     * @param string $field
     * @param string $order
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getUserOrder($timeStr = '', $field = "create_date_day", $order = 'desc', $page = 1, $limit = 10)
    {
        $db_order = new OrderModel();
        $data = $db_order->getOrderAnalysis($timeStr, $field, $order, $page, $limit);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

}